/*
 * Algo.java
 *
 * Created on December 14, 2000, 8:40 PM
 */

package com.gemalto.Kryptik.Algo;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import com.gemalto.Kryptik.Algo.*;
import com.gemalto.Kryptik.Util.*;
import com.gemalto.Kryptik.Gui.*;
 
/**
 * 
 * @author  STURC
 * @version 
 */
public class MacAlgo extends KeyedAlgo implements ISignature {

	private String cipherName;	
	private String provider;
	private String mode;
	private String padding;
	private Mac mac;

	  /** Creates new Algo */
	  public MacAlgo(String algoName, String shortName, String cipherName) { //throws AlgoException {
	  
		this.algoName  = algoName;
		this.shortName = shortName;
		this.cipherName= cipherName;
	
		try {
			mac = Mac.getInstance(cipherName,"BC");
		} catch ( NoSuchAlgorithmException e) {
			//throw new AlgoException(e.getMessage());
			System.err.println(e.getMessage());
		} catch (NoSuchProviderException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
      }


	 

	public String doSignature(String plainText) throws AlgoException {
	    String t = Util.omitSpaces(plainText);        

	    try {
				SecretKeySpec keySpec= new SecretKeySpec(Hex.fromString(getKey()), cipherName);
				mac.init(keySpec);				
				String sign =Hex.toString(mac.doFinal(Hex.fromString(t)));
				return sign;
	    } catch (Exception e){ 
	    	throw new AlgoException(e.getMessage());
			//System.err.println(e);
			//return null;
	    }
	}

}


// vim:tabstop=4:nowrap 
